package com.vnt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vnt.common.Result;
import com.vnt.feign.entity.SysUser;
import com.vnt.feign.entity.User;
import com.vnt.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class Controller {

    @GetMapping("/getById/{id}")
    public User getById(@PathVariable("id") String id){
        User user = new User();
        user.setUsername("user "+id);
        user.setPassword("password 123456");
        return user;
    }

    @PostMapping("/login")
    @ResponseBody
    public Result<?> login(@RequestBody User user){
        return Result.success("ok");

    }

    @GetMapping("/")
    public Result<?> index(){
        return Result.success("user login");
    }

    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/list")
    public Result<?> userListBySearch(@RequestParam(required = false) String userName,
                                      @RequestParam(required = false) String nickName,
                                      @RequestParam(required = false) String phone,
                                      @RequestParam(required = false) String email,
                                      @RequestParam(required = false) String role,
                                      @RequestParam(required = false) Integer page,
                                      @RequestParam(required = false) Integer perPage){
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        if(userName!=null){
            query.like("user_name",userName);
        }
        if(nickName!=null){
            query.like("nick_name",nickName);
        }
        if(phone!=null){
            query.like("phone",phone);
        }
        if(email!=null){
            query.like("email",email);
        }
        if(role!=null){
            query.like("role",role);
        }
        //return Result.success(userService.list(query));
        return Result.success(userService.page(new Page<>(page,perPage),query));
    }

    @GetMapping("/selectById/{id}")
    public Result<?> selectById(@PathVariable Long id){
        return Result.success(userService.getById(id));
    }

    @PutMapping("/updateById")
    @ResponseBody
    public Result<?> updateById(@RequestBody SysUser user){
        return Result.success(userService.updateById(user));
    }

    /**
     * 通过用户 Id 进行删除
     */
    @DeleteMapping("/deleteById/{id}")
    public Result<?> deleteById(@PathVariable Long id){
        return Result.success(userService.removeById(id));
    }
}
